Simplify and fix reading of ASCII images.
authorMatthias Clasen <mclasen@redhat.com>
Wed, 6 Sep 2006 05:11:20 +0000 (05:11 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Wed, 6 Sep 2006 05:11:20 +0000 (05:11 +0000)
2006-09-06  Matthias Clasen  <mclasen@redhat.com>

* io-pnm.c: Simplify and fix reading of ASCII images.

gdk-pixbuf/ChangeLog
gdk-pixbuf/io-pnm.c

index 58029e44fed54358f177f4b5ab76ceffdf08ea71..b72ed39b2f4fb1456b05281cb63028a6bdf9569e 100644 (file)
@@ -1,3 +1,7 @@
+2006-09-06  Matthias Clasen  <mclasen@redhat.com>
+
+       * io-pnm.c: Simplify and fix reading of ASCII images. 
+
 2006-08-17  Matthias Clasen  <mclasen@redhat.com>
 
        * === Released 2.10.2 ===
index 3af9794c252d994b81ef3682998f839b4a4fb064..af40f0fe25248b3f4868a95fcb6e542beb6cbf87 100644 (file)
@@ -527,109 +527,90 @@ pnm_read_raw_scanline (PnmLoaderContext *context)
 }
 
 static gint
-pnm_read_ascii_scanline (PnmLoaderContext *context)
+pnm_read_ascii_mono_scanline (PnmLoaderContext *context)
 {
        PnmIOBuffer *inbuf;
-       guint offset;
-       guint value, numval, i;
-       guchar data;
-       guchar mask;
-       guchar *dptr;
+       guint value;
        gint retval;
+       guchar *dptr;
        gint max_length;
-       
-       g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
-       
-       data = mask = 0;
-       
-       inbuf = &context->inbuf;
-       
-       context->dptr = context->pixels + context->output_row * context->rowstride;
-       
-       switch (context->type) {
-       case PNM_FORMAT_PBM:
+
+       if (context->type == PNM_TYPE_PBM)
                max_length = 1;
-               numval = MIN (8, context->width - context->output_col);
-               offset = context->output_col / 8;
-               break;
-       case PNM_FORMAT_PGM:
-               max_length = -1;
-               numval = 1;
-               offset = context->output_col;
-               break;
-       case PNM_FORMAT_PPM:
+       else
                max_length = -1;
-               numval = 3;
-               offset = context->output_col * 3;
-               break;
-               
-       default:
-               g_set_error (context->error,
-                            GDK_PIXBUF_ERROR,
-                            GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
-                            _("PNM image format is invalid"));
 
-               return PNM_FATAL_ERR;
+       inbuf = &context->inbuf;
+
+       context->dptr = context->pixels + context->output_row * context->rowstride;
+
+       dptr = context->dptr + context->output_col * 3;
+
+       while (TRUE) {
+               retval = pnm_read_next_value (inbuf, max_length, &value, context->error);
+               if (retval != PNM_OK)
+                       return retval;
+
+               if (context->type == PNM_TYPE_PBM) {
+                       value = value ? 0 : 0xff;
+               }
+               else {
+                       /* scale the color up or down to an 8-bit color depth */
+                       if (value > context->maxval)
+                               value = 255;
+                       else
+                               value = (guchar)(255 * value / context->maxval);
+               }
+                       
+               *dptr++ = value;
+               *dptr++ = value;
+               *dptr++ = value;
+
+               context->output_col++;
+
+               if (context->output_col == context->width) {
+                       context->output_col = 0;
+                       context->output_row++;
+                       break;
+               }
        }
+
+       return PNM_OK;
+}
+
+static gint
+pnm_read_ascii_color_scanline (PnmLoaderContext *context)
+{
+       PnmIOBuffer *inbuf;
+       guint value, i;
+       guchar *dptr;
+       gint retval;
+       
+       inbuf = &context->inbuf;
+       
+       context->dptr = context->pixels + context->output_row * context->rowstride;
        
-       dptr = context->dptr + offset + context->scan_state;
+       dptr = context->dptr + context->output_col * 3 + context->scan_state;
        
        while (TRUE) {
-               if (context->type == PNM_FORMAT_PBM) {
-                       mask = 0x80;
-                       data = 0;
-                       numval = MIN (8, context->width - context->output_col);
-               }
-               
-               for (i = context->scan_state; i < numval; i++) {
-                       retval = pnm_read_next_value (inbuf, max_length, 
-                                                     &value, context->error);
+               for (i = context->scan_state; i < 3; i++) {
+                       retval = pnm_read_next_value (inbuf, -1, &value, context->error);
                        if (retval != PNM_OK) {
                                /* save state and return */
                                context->scan_state = i;
                                return retval;
                        }
                        
-                       switch (context->type) {
-                       case PNM_FORMAT_PBM:
-                               if (value)
-                                       data |= mask;
-                               mask >>= 1;
-                               
-                               break;
-                       case PNM_FORMAT_PGM:
-                       case PNM_FORMAT_PPM:
-                               /* scale the color up or down to an 8-bit color depth */
-                               if (value > context->maxval)
-                                       *dptr++ = 255;
-                               else
-                                       *dptr++ = (guchar)(255 * value / context->maxval);
-                               break;
-                       default:
-                               g_set_error (context->error,
-                                            GDK_PIXBUF_ERROR,
-                                            GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
-                                            _("PNM image format is invalid"));
-                               return PNM_FATAL_ERR;
-                               break;
-                       }
+                       if (value > context->maxval)
+                               *dptr++ = 255;
+                       else
+                               *dptr++ = (guchar)(255 * value / context->maxval);
                }
                
                context->scan_state = 0;
-               
-               if (context->type == PNM_FORMAT_PBM) {
-                       *dptr++ = data;
-                       context->output_col += numval;
-               } else {
-                       context->output_col++;
-               }
+               context->output_col++;
                
                if (context->output_col == context->width) {
-                       if (context->type == PNM_FORMAT_PBM)
-                               explode_bitmap_into_buf (context);
-                       else if (context->type == PNM_FORMAT_PGM)
-                               explode_gray_into_buf (context);
-                       
                        context->output_col = 0;
                        context->output_row++;
                        break;
@@ -659,8 +640,12 @@ pnm_read_scanline (PnmLoaderContext *context)
                break;
        case PNM_FORMAT_PBM:
        case PNM_FORMAT_PGM:
+               retval = pnm_read_ascii_mono_scanline (context);
+               if (retval != PNM_OK)
+                       return retval;
+               break;          
        case PNM_FORMAT_PPM:
-               retval = pnm_read_ascii_scanline (context);
+               retval = pnm_read_ascii_color_scanline (context);
                if (retval != PNM_OK)
                        return retval;
                break;